package com.chuqiyun.authorization.controller.API.v1;

import com.alibaba.fastjson2.JSONObject;
import com.chuqiyun.authorization.annotation.PublicSysApiCheck;
import com.chuqiyun.authorization.entity.Product;
import com.chuqiyun.authorization.service.CommodityService;
import com.chuqiyun.authorization.service.ProductService;
import com.chuqiyun.authorization.utils.ModUtil;
import com.chuqiyun.authorization.utils.ResponseResult;
import com.chuqiyun.authorization.utils.exception.UnauthorizedException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.UUID;

/**
 * @author mryunqi
 * @date 2023/5/8
 */
@RestController
public class BaseMoFangApiController {
    @Resource
    private CommodityService commodityService;
    @Resource
    private ProductService productService;
    @PublicSysApiCheck
    @GetMapping("/api/v1/api-test")
    public ResponseResult<String> apiTest(){
        return ResponseResult.ok();
    }

    @PublicSysApiCheck
    @PostMapping("/api/v1/create")
    public ResponseResult<String>create(@RequestBody JSONObject body) throws UnauthorizedException {
        String domain = body.getString("domain");
        String ip = body.getString("ip");
        String authType = body.getString("auth_type");
        long regDate = body.getLong("regdate");
        long nextDueDate = body.getLong("nextduedate");
        String code = UUID.randomUUID().toString();
        String productName = commodityService.getById(body.getInteger("product_id")).getName();
        Product product = new Product();
        product.setCommodityId(body.getInteger("product_id"));
        product.setUserUuid("API");
        product.setName(productName);
        product.setProductCode(code);
        product.setOpeningDate(regDate);
        product.setExpirationDate(nextDueDate);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("domain", domain);
        jsonObject.put("ip",ip);
        jsonObject.put("auth_type",authType);
        product.setAuthData(jsonObject.toJSONString());
        if (productService.save(product)){
            return ResponseResult.ok(code);
        }
        return ResponseResult.fail();
    }

    @PublicSysApiCheck
    @GetMapping("/api/v1/sync")
    public ResponseResult<JSONObject> sync(@RequestParam(value = "code") String code) throws UnauthorizedException{
        Product product = productService.getProductByCode(code);
        String initData = product.getAuthData();
        JSONObject jsonObject = JSONObject.parseObject(initData);
        jsonObject.put("regDate",product.getOpeningDate());
        jsonObject.put("nextDueDate",product.getExpirationDate());
        return ResponseResult.ok(jsonObject);
    }

    @PublicSysApiCheck
    @PostMapping("/api/v1/renew")
    public ResponseResult<String> renew(@RequestBody JSONObject body) throws UnauthorizedException {
        String code = body.getString("code");
        long nextDueDate = body.getLong("nextDueDate");
        Product product = productService.getProductByCode(code);
        long oldNextDueDate = product.getExpirationDate();
        long time = System.currentTimeMillis()/1000;
        if (time < oldNextDueDate){
            product.setExpirationDate(nextDueDate);
            if (productService.updateById(product)){
                return ResponseResult.ok();
            }else {
                return ResponseResult.fail();
            }
        }
        product.setState(0);
        product.setExpirationDate(nextDueDate);
        if (productService.updateById(product)){
            return ResponseResult.ok();
        }else {
            return ResponseResult.fail();
        }
    }

    @PublicSysApiCheck
    @PostMapping("/api/v1/suspendAccount")
    public ResponseResult<String> suspendAccount(@RequestBody JSONObject body) throws UnauthorizedException{
        String code = body.getString("code");
        Product product = productService.getProductByCode(code);
        if (product == null){
            return ResponseResult.ok();
        }
        product.setState(1);
        if (productService.updateById(product)){
            return ResponseResult.ok();
        }else {
            return ResponseResult.fail();
        }
    }

    @PublicSysApiCheck
    @PostMapping("/api/v1/unSuspendAccount")
    public ResponseResult<String> unSuspendAccount(@RequestBody JSONObject body) throws UnauthorizedException{
        String code = body.getString("code");
        Product product = productService.getProductByCode(code);
        if (product == null){
            return ResponseResult.ok();
        }
        product.setState(0);
        if (productService.updateById(product)){
            return ResponseResult.ok();
        }else {
            return ResponseResult.fail();
        }
    }

    @GetMapping("/api/v1/authVerify")
    public ResponseResult<String> authVerify(@RequestParam(value = "code") String code,
                                             @RequestParam(value = "domain") String domain,
                                             @RequestParam(value = "ip") String ip) throws UnauthorizedException{
        Product product = productService.getProductByCode(code);
        if (ModUtil.isNull(product)){
            return ResponseResult.fail(ResponseResult.RespCode.UNAUTHORIZED);
        }
        if (product.getState() == 1){
            return ResponseResult.fail(ResponseResult.RespCode.UNAUTHORIZED);
        }
        JSONObject jsonData = JSONObject.parseObject(product.getAuthData());

        if (!jsonData.getString("ip").equals(ip)){
            return ResponseResult.fail(ResponseResult.RespCode.UNAUTHORIZED);
        }

        String oneDomain = ModUtil.getTopLevelDomain(domain);

        if (!oneDomain.equals(jsonData.getString("domain"))){
            return ResponseResult.fail(ResponseResult.RespCode.UNAUTHORIZED);
        }
        return ResponseResult.ok();
    }

}
